require(['/js/config.js'],() => {
    require(['template','request','zoom','jquery','header','footer'],(template,req,zoom,)=>{
        class infoPage{
			constructor() {
			    this.init()
			}
			init(){
				this.getList()
			}
			getList(){
				const id = window.location.search.slice(1)
				req.getInfoData(id)
					.then(res=>{
						const data = res.data.detail
						this.infoBrash(data,id)
					})
			}
			infoBrash(res,id){
				const{
					title,
					coupon,
					originPrice,
					price,
					accessoryHints,
					taobaoCid,
					desc=res.descContentList.slice(3),
					image,
					photo
				}=res
				$(".infoBox").html(template('infoList',{ 
					title,
					coupon,
					originPrice,
					price,
					accessoryHints,
					taobaoCid,
					desc,
					image,
					photo
				}))
				$(".zoom-image").elevateZoom({
					gallery: 'showUl'
				})
				this.activeBrash()
				this.addCar([{id,image,title,originPrice,price}])
			}
			activeBrash(){
				//选择尺码高亮
				let num = 1
				$(".sizeBox").on("click","span",function(){
					num = 1
					$(this).toggleClass("active").siblings().removeClass("active")
					$(".numInput").val(num)
				})
				//选择数量

				$(".numUl").on("click",(e)=>{
					if(e.target.className==="jianBtn"){
						if(num>1){
							$(".numInput").val(num-1)
						num--
						}
					}else if(e.target.className==="addBtn"){
						$(".numInput").val(num+1)
						num++
					}
				})
			}
			addCar([{id,image,title,originPrice,price}]){
				
				$(".addBox").on("click",".addCar",()=>{
					let num = $(".numInput").val()-0
				console.log(num)
				console.log(typeof num)
					//获取商品信息
					let info=null
					if(localStorage.getItem('info')){
						info = JSON.parse(localStorage.getItem('info'))
						let flag = false
						info.forEach(function(item){
							if(item.id == id){
								item.count =item.count+num
								flag = true
							}
						});
						if(flag){
						}else{
							let obj = {id,image,title,originPrice,price,count:num,checked:true}
							info.push(obj)
						}
					}else{
						info=[{id,image,title,originPrice,price,count:num,checked:true}]
					}
					localStorage.setItem('info',JSON.stringify(info))
					//淡入淡出提醒
					$(".addSuccess").stop().fadeIn(500,()=>{
						$(".addSuccess").delay(500).fadeOut(500, ()=>{

						});
					})
					return false
				})
			}
		}
		return new infoPage()
    })
})

